<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%@page import="util.*"%>
<%@page import="dal.*"%>
<%@ page import="model.*,java.util.*"%>
<%
	GenericDal<UnitModel, UnitModelExt> dal = new GenericDal<UnitModel, UnitModelExt>(
			new UnitModel());
	UnitModelExt queryCount = new UnitModelExt();
	int count = dal.count(queryCount);

// 	int pageIndex = 1;
// 	if (request.getParameter("pageIndex") != null
// 			&& !request.getParameter("pageIndex").isEmpty()) {
// 		try {
// 			pageIndex = Integer.parseInt(request
// 					.getParameter("pageIndex"));
// 		} catch (Exception e) {
// 		}
// 	}

// 	int pageSize = ConfigUtil.unitListPageSize;
// 	if (request.getParameter("pageSize") != null
// 			&& !request.getParameter("pageSize").isEmpty()) {
// 		try {
// 			pageSize = Integer.parseInt(request
// 					.getParameter("pageSize"));
// 		} catch (Exception e) {
// 		}
// 	}
	int pageIndex = MiscUtil.getIntUrlParam(request, "pageIndex", 1);
	int pageSize = MiscUtil.getIntUrlParam(request, "pageSize", 10);
	int noOfPage = count / pageSize;
	if (count % pageSize > 0) {
		noOfPage++;
	}

	UnitModelExt query = new UnitModelExt();
	query.setLimit(pageSize);
	query.setOffset((pageIndex - 1) * pageSize);
	List<UnitModel> list = dal.selectList(query);

	boolean embedded = MiscUtil.getBoolUrlParam(request, "embedded", false);
%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Unit List</title>
<%
	if (!embedded) {
%>
<link rel="stylesheet" href="css/common.css" />
<link rel="stylesheet" href="css/inputForm.css" />
<link rel="stylesheet" href="css/listForm.css" />
<link rel="stylesheet" href="css/jquery-ui-1.8.14.custom.css" type="text/css" />
<link rel="stylesheet" href="css/hyjack.css" type="text/css" />
<link rel="stylesheet" href="css/datepicker.css" type="text/css" />
<link rel="stylesheet" href="css/themes/ui-lightness/jquery.ui.all.css" />
<link rel="stylesheet" href="css/jquery-ui-timepicker.css" type="text/css" />
<link rel="stylesheet" href="css/jquery-datepicker.css" />
<link rel="stylesheet" href="css/jquery-dialog.css" />

<script language="javascript" type="text/javascript" src="js/datepicker.js"></script>
<script language="javascript" type="text/javascript" src="js/jquery-1.6.1.js"></script>
<script type="text/javascript" src="js/jquery-ui-1.8.14.custom.min.js"></script>
<script language="javascript" type="text/javascript" src="js/jquery.hyjack.select.js"></script>
<script language="javascript" type="text/javascript" src="js/jquery.ui.core.min.js"></script>
<script language="javascript" type="text/javascript" src="js/jquery.ui.widget.min.js"></script>
<script language="javascript" type="text/javascript" src="js/jquery.ui.timepicker.js"></script>
<script language="javascript" type="text/javascript" src="js/jquery.ui.datepicker.js"></script>
<script language="javascript" type="text/javascript" src="js/datepicker.js"></script>
<script language="javascript" type="text/javascript" src="js/jquery.hyjack.js"></script>
<script language="javascript" type="text/javascript" src="js/ajaxQuickInsert.js"></script>
<script language="javascript" type="text/javascript" src="js/jquery.numeric.js"></script>
<script language="javascript" type="text/javascript" src="js/jquery.ui.tools.min.js"></script>
<script language="javascript" type="text/javascript" src="js/common.js"></script>

<script language="javascript" type="text/javascript">
$(document).ready(pageLoaded);
function pageLoaded()
{
	initTooltip();	
	initDeleteConfirmation();
} 
var requiredInputs = [['name','name_required']];
var intInputs = [];
var doubleInputs = [];
var dateInputs = [];
var timeInputs = [];


function checkInputs() {
	var valid = true;

	if (checkRequiredInputs(requiredInputs) == false) {
		valid = false;
	}
// 	if (checkIntInputs(intInputs) == false) {
// 		valid = false;
// 	}
// 	if (checkDoubleInputs(doubleInputs) == false) {
// 		valid = false;
// 	}
// 	if (checkDateInputs(dateInputs) == false) {
// 		valid = false;
// 	}
// 	if (checkTimeInputs(timeInputs) == false) {
// 		valid = false;
// 	}

	// SPECIFIC VALIDATE CODE HERE		

	return valid;
}
	
<%
	}
%>	
</script>
</head>
<body>
	
	<%
		if (!embedded) {
	%>
	<div class="divContainer">
		<jsp:include page="menu.jsp"></jsp:include>
	<form id="inputForm" action="unit?mode=insert" method="post" onSubmit="return checkInputs()">
		<table class="tblInput">
			<tr>
				<td class="inputHeader">Add New Unit</td>
			</tr>
			<tr>
					<td style="padding-left: 400px; padding-right: 400px;">
						<table cellspacing="0px">
			<tr class="trField">
							<td class="tdFieldName">Name</td>
							<td class="tdFieldInput"><input class="inputText" type="text" id="name" name="name" maxlength="255" title="Name of new unit. Note: Required."  onblur="validateRequiredInput('name');"/></td>
							<td class="tdErrMsg">
								<span id="name_required" class="errMsg" style="display: none;" title="Name is required.">(!)</span> 
							</td>
						</tr>
		
			<tr class="trInputBottom">
				<td class="trInputBottom" colspan="3">
					<table class="tblSubmit" align="center">
						<tr class="trSubmit">
							<td class="tdSubmit"><input class="btnSubmit" type="submit" value="Submit" title="Click to add unit." />
							</td>
						</tr>
					</table>
				</td>
			</tr>
		</table>
				</td>
			</tr>
		</table>
	</form>
	</div>
	<div class="divContainer">
	<%}%>
	
	<div class="divListHeaderBar">NUMBER OF UNITS: <%=count%></div>
	<table class="tblList">
		<tr class="tblListHeader">
			<th class="thListHeader">Unit</th> 
			
			<th class="thListHeader Id">Id</th>
			<th class="thListHeader">Name</th>
		</tr>

		<%
			int size = list.size();
			for (int i = 0; i < size; i++) {
				UnitModel obj = list.get(i);
		%>

		<tr class="trListItem">
			<td class="tdListInt">
				<span><%=i + 1%></span>
				<br/>
				<span class="spanEdit"><a href="unit?mode=edit&id=<%=obj.getId()%>">Edit</a></span>
				<br/>
				<span class="spanEdit"><a class="hrefDelete" href="unit?mode=delete&id=<%=obj.getId()%>"  onclick="confirmToDelete();">Delete</a></span>
			</td>
			<td class="tdList"><%=obj.getIdToString()%></td>
			<td class="tdListText projectName"><%=obj.getNameToString()%></td>
			
		</tr>
		<%
			}
		%>
	</table>
	<br />
	<%
		if(noOfPage>1)
		{
	%>
	<table class="tblPaging">
		<tr class="trPaging">
			<td class="tdPaging">
				<%
					for (int i = 1; i <= noOfPage; i++) {
						if (i == pageIndex) {
				%> <span class="lblPaging"><%=i%></span> 
				<%	} else { %> 
 				<a class="hrefPaging" href="unitList.jsp?pageIndex=<%=i%>&pageSize=<%=pageSize%>"><%=i%></a>
				<%	 	}
					}
				%>
			</td>
		</tr>
	</table>
	<%} %>
	<%if(!embedded){ %> </div> <%} %>

</body>
</html>